package com.hbt.myplus.controller;


import com.hbt.myplus.model.Img;
import com.hbt.myplus.model.Reg;
import com.hbt.myplus.model.Services;
import com.hbt.myplus.service.impl.ImgServiceImpl;
import com.hbt.myplus.utils.OSSUtil;
import com.hbt.myplus.utils.ResponseWeb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    ImgServiceImpl imgService;

    @PostMapping("/service_imgs")
    public Map<String,Object>  serviceImgs(@RequestParam("file") MultipartFile file){
        OSSUtil ossClient=new OSSUtil();
        Map<String, Object> m = new HashMap<>();
        if (file.isEmpty()) {
            m.put("error","上传文件不能为空");
        }
        String newsUrl = "";
        if (!((MultipartFile) file).isEmpty()) {
            try {
                //将文件上传
                String name = ossClient.uploadImg2Oss(file);
                //获取文件的URl地址  以便前台  显示
                String imgUrl = ossClient.getImgUrl(name);
                m.put("name",file.getOriginalFilename());
                m.put("url",imgUrl);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            m.put("error","上传图片不可为空");
        }
        return m;
    }

    @PostMapping("/img/services/{service_id}")
    @Transactional
    public ResponseWeb uploadServiceImg(@RequestParam("file") MultipartFile file, @PathVariable("service_id") String service_id_str){
        Integer service_id = Integer.parseInt(service_id_str);
        System.out.println("service_id:"+service_id);
        if (service_id == null){
            return ResponseWeb.fail("服务id不能为空");
        }
        OSSUtil ossClient=new OSSUtil();
        Map<String, Object> m = new HashMap<>();
        if (file.isEmpty()) {
            m.put("error","上传文件不能为空");
        }
        if (!((MultipartFile) file).isEmpty()) {
            try {
                //将文件上传
                String name = ossClient.uploadImg2Oss(file);
                //获取文件的URl地址  以便前台  显示
                String imgUrl = ossClient.getImgUrl(name);
                m.put("name","图片");
                m.put("url",imgUrl);

                //将上传的图片与挂号单号关联
                Img img = new Img();
                img.setName(file.getOriginalFilename());
                img.setTbname("services");
                img.setInsId(service_id);
                img.setUrl(imgUrl);
                img.setField(Services.DESC_IMG);
                imgService.save(img);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            m.put("error","上传图片不可为空");
        }
        return new ResponseWeb(m);
    }

    @PostMapping("/report/{regId}")
    @Transactional
    public ResponseWeb uploadReport(@RequestParam("file") MultipartFile file, @PathVariable("regId") String regIdStr){
        System.out.println("regId:"+regIdStr);
        Integer regId = null;
        try {
            regId = Integer.parseInt(regIdStr);
            if (regIdStr == null){
                return ResponseWeb.fail("挂号单号不能为空");
            }
        }catch (Exception e){
            return ResponseWeb.fail("挂号单号不能为空");
        }
        OSSUtil ossClient=new OSSUtil();
        Map<String, Object> m = new HashMap<>();
        if (file.isEmpty()) {
            m.put("error","上传文件不能为空");
        }
        String newsUrl = "";
        if (!((MultipartFile) file).isEmpty()) {
            try {
                //将文件上传
                String name = ossClient.uploadImg2Oss(file);
                //获取文件的URl地址  以便前台  显示
                String imgUrl = ossClient.getImgUrl(name);
                m.put("name",file.getOriginalFilename());
                m.put("url",imgUrl);

                //将上传的图片与挂号单号关联
                Img img = new Img();
                img.setName(file.getOriginalFilename());
                img.setTbname("reg");
                img.setInsId(regId);
                img.setUrl(imgUrl);
                img.setField(Reg.REPORT);
                imgService.save(img);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            m.put("error","上传图片不可为空");
        }
        return null;
    }


    @DeleteMapping("/remove/{id}")
    public ResponseWeb removeFile(@PathVariable("id")Integer id){

        return new ResponseWeb(imgService.removeById(id));
    }


    @PostMapping("/file")
    public Map<String,Object>  uploadFile(@RequestParam("file") MultipartFile file){
        OSSUtil ossClient=new OSSUtil();
        Map<String, Object> m = new HashMap<>();
        if (file.isEmpty()) {
            m.put("error","上传文件不能为空");
        }
        String newsUrl = "";
        if (!((MultipartFile) file).isEmpty()) {
            try {
                //将文件上传
                String name = ossClient.uploadImg2Oss(file);
                //获取文件的URl地址  以便前台  显示
                String imgUrl = ossClient.getImgUrl(name);
                m.put("name",file.getOriginalFilename());
                m.put("url",imgUrl);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            m.put("error","上传图片不可为空");
        }
        return m;
    }




}
